<?php

namespace login\controller;

require_once('/login/model/AddUserDAL.php');
require_once("/login/model/User.php");
require_once("/login/view/HTMLView.php");
require_once("/login/view/Userinfo.php");

class AddUserController {
	/**
	 * @return string HTML
	 */
	public function AddUser() {
		$usrInfoView = new \login\view\UserInfo();
		$user = $usrInfoView->getNewUserInfo();
		$pageView = new \login\view\HTMLView();

		try {
			$user->checkUserCredentials();
		} catch (\Exception $e) {
			$pageView->setErrorMessage($e->getMessage());
			$pageView->setRegisterValue();
			return $pageView->getRegisterPage();
		}
		$mysqli = new \mysqli("mysql08.citynetwork.se", "119958-mg18906",
							  "quizzer50", "119958-quizzer");
		try {
			\login\model\AddUserDAL::AddUser($user->getUsername(), $user->getPassword(), $mysqli);
			$mysqli->close();
		} catch (\Exception $e) {
			if ($e->getMessage() == "1") {
				$pageView->setUserExistsMessage();
			}
			$mysqli->close();
			return $pageView->getRegisterPage();
		}
		return $pageView->getLoginPage();
	}
}